Java 自动生成16位Ling类型id

下载地址

自动生成16位Ling类型id

使用

1
ID16NumberGenerator.get16NumberID()

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

package com.wangjikai.util;

import org.apache.commons.lang3.time.DateFormatUtils;

/*
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
*/
import java.util.*;

public class ID16NumberGenerator {
private static ID16NumberGenerator instance = new ID16NumberGenerator();
//private Logger logger = LoggerFactory.getLogger(this.getClass());
private long time = 0L;
private int seq = 101;

private ID16NumberGenerator() {
}

public static Long get16NumberID() {
ID16NumberGenerator var0 = instance;
synchronized(instance) {
return instance.generate16Id();
}
}

public static int getNRdmNumber(int n) {
if (n > 10) {
n = 10;
} else if (n < 1) {
n = 1;
}

int min = (int)Math.pow(10.0D, (double)(n - 1));
int max = (int)Math.pow(10.0D, (double)n) - 1;
return getVectorNum(max, min);
}

public static Set<Long> get16NumberID(int count) {
int size = count > 0 ? count : 1;
TreeSet ids = new TreeSet();

while(ids.size() < size) {
ids.add(get16NumberID());
}

return ids;
}

private static synchronized int getVectorNum(int max, int min) {
Random random = new Random();
int s1 = random.nextInt(max) % (max - min + 1) + min;
return s1;
}



public static String getPrchaseNum() {
String timeString = DateFormatUtils.format(new Date(), "yyMMdd");
String result = "CG" + timeString + getVectorNum(999999, 100000);
return result;
}

private Long generate16Id() {
long currentTime = System.currentTimeMillis();
if (currentTime == this.time) {
++this.seq;
} else {
this.seq = 101;
this.time = currentTime;
}

if (this.seq > 999) {
try {
Thread.sleep(1L);
} catch (InterruptedException var4) {
//this.logger.warn(var4.getMessage(), var4);
}

return this.generate16Id();
} else {
return this.time * 1000L + (long)this.seq;
}
}
/**
* 获取uuid
*
* @return uuid
* */
public static String getUuid(){
// uuid
String uuid = UUID.randomUUID().toString();
return uuid;
}

public static void main(String[] arg) throws InterruptedException {

for(int i = 0; i < 10; ++i) {
System.out.println(ID16NumberGenerator.get16NumberID());
}
for(int i = 0; i < 10; ++i) {
// System.out.println(getNRdmNumber(2));
Thread.sleep(1);
System.out.println(ID16NumberGenerator.get16NumberID());
}
System.out.println(ID16NumberGenerator.get16NumberID());


Set<Integer> ints = new TreeSet();

for(int i = 0; i < 10; ++i) {
System.out.println(getNRdmNumber(2));
}

Iterator i$ = ints.iterator();

while(i$.hasNext()) {
Integer num = (Integer)i$.next();
System.out.println(num);
}

System.out.println(ints.size());
}
}
继开 wechat
欢迎加我的微信,共同交流技术